package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import org.junit.Test;

import java.util.Random;

/**
 * @author Fengkai  输入两个字符串 S 和 L，都只包含英文小写字母。S 长度<=100，L 长度<=500,000。
 * @creat 2023/3/29
 */
public class FindSLStr {

       public static void main(String[] args){
//           System.out.println(myMethod("fgh", "abcde"));
           System.out.println((int) 'a');
           System.out.println((int) 'z');
           System.out.println((char) 98);
           System.out.println((int) 'Z');
           Random random = new Random();
           System.out.println(random.nextInt(26) + 97);
           for (int i = 0; i < 500; i++) {
               //生成S L
               String S  = "",L = "";

               for (int j = 0; j < (int)(Math.random()*100 + 1); j++) {
                   S += (char)(random.nextInt(26) + 97);
               }

               for (int j = 0; j < (int)(Math.random()*50000 + 1); j++) {
                   L += (char)(random.nextInt(26) + 97);
               }
               System.out.println("S:" + S + ",L:" + L);
               System.out.println(myMethod(S, L));
           }

       }


    public static int myMethod(String S, String L){
        char[] charsS = S.toCharArray();
        char[] charsL = L.toCharArray();
        int index = 0;
        int out = -1;
        out:for (int i = 0; i < charsS.length ; i++) {
            for (int j = index; j < charsL.length; j++) {
                if (charsS[i] == charsL[j]) {
                    index = j + 1;
                    if (i == charsS.length - 1){
                        out = j;
                        break ;
                    }
                   continue out;
                }
            }
        }
        return out;
    }

    //其他解法  有问题的
    public static int solve(String S, String L) {
        int lastIdx = -1;
        for (int i = 0; i < S.length(); i++) {
            boolean findFlag = false;
            for (int j = lastIdx + 1; j < L.length(); j++) {
                if (L.charAt(j) == S.charAt(i)) {
                    lastIdx = j;
                    findFlag = true;
                    break;
                }
            }
            if (!findFlag) {
                break;
            }
        }
       return lastIdx;
    }

    @Test
    public void test(){
        System.out.println(solve("czxhmi", "ebonsegvbudraaitxbnqditkcshjaieecewddgdlyfsdtoxtytlzdwypaintfyihyhmnybtqggrjshtyosedtvwmebuwwesnchfkrponckyfkrfccxcnnxqiqrybjwsetoodldbdwbsobkundammafc"));
        System.out.println("ebonsegvbudraaitxbnqditkcshjaieecewddgdlyfsdtoxtytlzdwypaintfyihyhmnybtqggrjshtyosedtvwmebuwwesnchfkrponckyfkrfccxcnnxqiqrybjwsetoodldbdwbsobkundammafc".charAt(113));
//        System.out.println(myMethod("czxhmi", "ebonsegvbudraaitxbnqditkcshjaieecewddgdlyfsdtoxtytlzdwypaintfyihyhmnybtqggrjshtyosedtvwmebuwwesnchfkrponckyfkrfccxcnnxqiqrybjwsetoodldbdwbsobkundammafc"));
    }
}
